package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XListView;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.entity.FinessStrategyBean;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by ASUS on 2018/5/16.
 */

public class FitnessStrategyActivity extends CommonActivity {
    @BindView(R.id.imageview)
    ImageView imageview;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.listview)
    XListView listview;
    @BindView(R.id.video_listview)
    XListView videoListview;
    private String url;
    private QuickAdapter<FinessStrategyBean.MapBean.StrategyDetailsBean> quickAdapter;
    private QuickAdapter<FinessStrategyBean.MapBean.VideoList> videoQuick;
    SensorManager sensorManager;
    JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activit_fitness_strategy);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            url = bundle.getString("linkurl");
        }

        quickAdapter = new QuickAdapter<FinessStrategyBean.MapBean.StrategyDetailsBean>(FitnessStrategyActivity.this, R.layout.item_fitness_strategy) {
            @Override
            protected void convert(BaseAdapterHelper helper, final FinessStrategyBean.MapBean.StrategyDetailsBean item) {
                helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle mBundle = new Bundle();
                        mBundle.putString("title", item.getStrategyDetailTitleName());
                        mBundle.putString("content", item.getContent());
                        startActivity(HtmlTextActivity.class, mBundle);
                    }
                });
                helper.setText(R.id.title, item.getStrategyDetailTitleName());
                TextView content = helper.getView(R.id.content);
                RichText.fromHtml(item.getContent()).into(content);
                ImageView photo = helper.getView(R.id.photo);
                Glide.with(context)
                        .load(item.getPhotoUrl())
                        .placeholder(R.mipmap.default_big_bg)
                        .centerCrop()
                        .into(photo);
            }
        };
        listview.setAdapter(quickAdapter);

        videoQuick = new QuickAdapter<FinessStrategyBean.MapBean.VideoList>(FitnessStrategyActivity.this, R.layout.item_video_img) {
            @Override
            protected void convert(BaseAdapterHelper helper, final FinessStrategyBean.MapBean.VideoList item) {
                JZVideoPlayerStandard video = helper.getView(R.id.video);
                video.setUp(item.getVideoUrl(), JZVideoPlayer.SCREEN_WINDOW_LIST, item.getVideoTitle());
                Glide.with(context)
                        .load(item.getPhotoUrl())
                        .centerCrop()
                        .into(video.thumbImageView);
            }
        };
        videoListview.setAdapter(videoQuick);
        videoListview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                JZVideoPlayer.onScrollAutoTiny(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        });

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        finalOkGo.request(RequestType.GET, Contacts.HOST_SERVER + url, null, url, new Callback<FinessStrategyBean>() {
            @Override
            public void onSuccess(FinessStrategyBean finessStrategyBean) {
                if (Constant.REQUEST_SUCCESS.equals(finessStrategyBean.getResult())) {
                    setTitle(finessStrategyBean.getMap().getStrategy().getTitleName());
                    Glide.with(context)
                            .load(finessStrategyBean.getMap().getStrategy().getPhotoUrl())
                            .placeholder(R.mipmap.default_big_bg)
                            .into(imageview);
                    RichText.fromHtml(finessStrategyBean.getMap().getStrategy().getContent()).into(content);
                    text.setText(finessStrategyBean.getMap().getStrategy().getStrategySubCategoryName());

                    quickAdapter.clear();
                    quickAdapter.addAll(finessStrategyBean.getMap().getStrategyDetails());
                    quickAdapter.notifyDataSetChanged();

                    videoQuick.clear();
                    videoQuick.addAll(finessStrategyBean.getMap().getVideos());
                    videoQuick.notifyDataSetChanged();

                } else {
                    showToast(finessStrategyBean.getMsg() + "");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
        }
    }
}
